package top.hidoctor.auscultation.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.tang.util.BigDecimalUtil;

import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.base.Constant;


public class SPlineView extends View {
    private static final String TAG = "view";
    private int width;
    private int heigh;

    //间距
    private float gridspace_width;

    //背景的画笔
    private Paint mPaint_bg;

    //折线的画笔
    private Paint mPaint_brokenline;
    //路径
    private Path mpath = new Path();
    private List<Float> mdata = new ArrayList<>();
    private int xMultiple;
    private Integer length;
    private Integer maxVal;
    private List<Integer> list;
    private int totalWidht;
    private int yMultiple;

    public SPlineView(Context context) {
        super(context);
        inite(context);
    }

    public SPlineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inite(context);
    }

    private void inite(Context context) {
        //灰色背景的画笔
        mPaint_bg = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_bg.setColor(Color.parseColor("#444444"));


        //折线的画笔
        mPaint_brokenline = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_brokenline.setColor(Color.parseColor("#65CA33"));
        mPaint_brokenline.setTextSize(18);
        mPaint_brokenline.setTextAlign(Paint.Align.CENTER);


        invalidate();
    }


    public void setDate(Integer length, Integer maxVal, List<Integer> item, int xMultiple, int yMultiple, int heigh) {
        this.xMultiple = xMultiple;
        this.yMultiple = yMultiple;
        this.length = length;
        this.heigh = heigh;
        this.maxVal = maxVal;
        this.list = item;
        totalWidht = (int) (Constant.width * Math.pow(2, xMultiple));

        for (int i = 0; i < item.size(); i++) {
            mdata.add((float) (item.get(i) * yMultiple) + maxVal);
        }
        gridspace_width = (float) BigDecimalUtil.div(totalWidht, length);

        requestLayout();//相当于调用onMeasure方法
        invalidate();//相当于调用onDraw方法
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制折线图的白色背景
        canvas.drawColor(Color.WHITE);
        //绘制灰色矩形区域
        canvas.drawRect(0, 0, width, heigh, mPaint_bg);

        for (int i = 0; i < mdata.size(); i++) {
            if (i == 0) {
                mpath.moveTo(0,
                        heigh - heigh * mdata.get(i) / (maxVal * 2));
                //开始时需要将path移动到要开始绘制的位置，否则默认从（0,0）开始，绘制path路径
//                mpath.moveTo(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i) / 100);
            }

            if (i != mdata.size() - 1) {
//                //根据圆点位置绘制折线
                canvas.drawLine(
                        gridspace_width * i, // X轴
                        heigh - heigh * mdata.get(i) / (maxVal * 2),
                        gridspace_width * (i + 1), // X轴+1
                        heigh - heigh * mdata.get(i + 1) / (maxVal * 2),
                        mPaint_brokenline);
            }

//            if (i == mdata.size() - 1) {
//                //绘制完最后一个点，path需要沿着纵轴向下到达heigh - brokenline_bottom 的位置再水平到达（10，heigh - brokenline_bottom ）的位置，最后闭合
//                mpath.quadTo(gridspace_width * i, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i) / 100, gridspace_width * i, heigh - brokenline_bottom);
//                mpath.quadTo(gridspace_width * i, heigh - brokenline_bottom, 10, heigh - brokenline_bottom);
//                mpath.close();
//            }
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        if (mdata.size() == 0) {
//            //通过调用onMeasure源码中的方法进行获得宽度，
//            //宽度的获得有三种模式，具体介绍见博客底部连接
//            width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
//        } else {
//            //根据数据条数设置宽度
//            width = gridspace_width * mdata.size() + 10;
//        }
//        heigh = 850;
        //设置底部白色空白的宽度
        if (list != null) {
            width = (int) BigDecimalUtil.mul(gridspace_width, list.size());
        }
        Log.e(TAG, "onMeasure:width " + width);
        Log.e(TAG, "onMeasure:gridspace_width " + gridspace_width);
        Log.e(TAG, "onMeasure:totalWidht " + totalWidht);
//        gridspace_heigh = (heighs - brokenline_bottom) / 4;
        setMeasuredDimension(width, heigh);
    }


}
